package com.craftcomputing.client.SearchResult;

import com.craftcomputing.client.Entry.EntryPanel;
import com.craftcomputing.client.SearchResult.Label.*;

import com.google.gwt.json.client.JSONArray;
import com.google.gwt.json.client.JSONValue;
import com.google.gwt.user.client.ui.*;
import com.google.gwt.user.client.ui.HTMLPanel;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.HasVerticalAlignment;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.*;

public abstract class SearchResult extends Composite{
	/**
	* @wbp.parser.constructor
	*/
	public SearchResult() {
		makeWidget();
	}
	public SearchResult(boolean searchWithin) {
		this.searchWithin = searchWithin;
		makeWidget();
	}
	public void makeWidget(){
		final VerticalPanel verticalPanel_1 = new VerticalPanel();
		initWidget(verticalPanel_1);
		verticalPanel_1.setSize("800px", "100%");
		
		final HorizontalPanel horizontalPanel = new HorizontalPanel();
		verticalPanel_1.add(horizontalPanel);
		horizontalPanel.setWidth("100%");

		final HorizontalPanel horizontalPanel_1 = new HorizontalPanel();
		horizontalPanel.add(horizontalPanel_1);
		horizontalPanel_1.setSize("100%", "100%");
		horizontalPanel.setCellHeight(horizontalPanel_1, "100%");
		horizontalPanel_1.setStyleName("gwt-SearchResults");
		horizontalPanel_1.setSpacing(4);
		horizontalPanel.setCellWidth(horizontalPanel_1, "60%");
		searchResultsLabel = new Label("Search Results for Entry");
		horizontalPanel_1.add(searchResultsLabel);
		searchResultsLabel.setStyleName("gwt-SearchResultsTitle");
		if(entryName() != null)
			searchResultsLabel.setText("Search Results for " + entryName());

		final VerticalPanel verticalPanel = new VerticalPanel();
		horizontalPanel.add(verticalPanel);
		verticalPanel.setWidth("100%");
		verticalPanel.setStyleName("gwt-SearchResults");

		searchWithinPanel = new SearchWithin(this, entryName());
		//setStyleName("gwt-SearchResultsPanel");
		//setWidth("796px");
		verticalPanel.add(searchWithinPanel);
		searchWithinPanel.setHeight("30px");
		verticalPanel.setCellHorizontalAlignment(searchWithinPanel, HasHorizontalAlignment.ALIGN_RIGHT);
		searchWithinPanel.setStyleName("gwt-SearchResults");

		final HorizontalPanel horizontalPanel_2 = new HorizontalPanel();
		verticalPanel.add(horizontalPanel_2);
		horizontalPanel_2.setSize("100%", "24px");
		horizontalPanel_2.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_CENTER);

		searchResultsSummary = new SearchResultsSummary(this, "Query", 10);
		horizontalPanel_2.add(searchResultsSummary);
		horizontalPanel_2.setCellVerticalAlignment(searchResultsSummary, HasVerticalAlignment.ALIGN_TOP);
		searchResultsSummary.setHeight("16px");
		horizontalPanel_2.setCellHorizontalAlignment(searchResultsSummary, HasHorizontalAlignment.ALIGN_RIGHT);
		verticalPanel_1.add(resultPanel);
		resultPanel.setSize("796px", "100%");
		verticalPanel_1.setCellHeight(resultPanel, "100%");
		verticalPanel_1.setCellVerticalAlignment(resultPanel, HasVerticalAlignment.ALIGN_TOP);
		final CodeSearchResultLabel codeSearchResultLabel = new CodeSearchResultLabel();
		resultPanel.add(codeSearchResultLabel);
		if(!searchWithin){
			//verticalPanel.remove(searchWithinPanel);
			verticalPanel_1.remove(horizontalPanel);
		}

	}
	public void setLabels(JSONArray jsonArray, String query){
		resultPanel.clear();
		//resultPanel.add(new SearchResultsSummary(this, query, jsonArray.size()));

		for (int i = 0; i < jsonArray.size(); ++i) {
        	resultPanel.add(makeEntryLabel(jsonArray.get(i)));
        }
		if(jsonArray.size() > 0)
			setVisible(true);
		else
			setVisible(false);
		if(jsonArray.size() < 10)
			searchWithinPanel.setVisible(false);
		else
			searchWithinPanel.setVisible(true);
	}
	public abstract Widget makeEntryLabel(JSONValue json);
	public abstract EntryPanel makeEntryPanel();
	public abstract String entryName();
	public abstract void doSearch(String tbText, boolean findCount);
	
	public void addLabel(JSONValue json){
		resultPanel.add(makeEntryLabel(json));
	}
	public void clearResultPanel()
	{
		resultPanel.clear();
	}
	private SearchWithin searchWithinPanel;
	private VerticalPanel resultPanel = new VerticalPanel();
	private boolean searchWithin = true;
	protected int offset = 0;
	protected String count = "";
	private SearchResultsSummary searchResultsSummary;
	private Label searchResultsLabel;
	
	public TextBox getTextBox(){return searchWithinPanel.getTextBox();}
	public String getCount(){return count;}
	public void setCount(String count){this.count = count; updateSummary(); }
	public int getOffset(){return offset;}
	public void setOffset(int offset){this.offset = offset; updateSummary(); }
	private void updateSummary(){
		if (searchResultsSummary != null) {
			searchResultsSummary.update();
			String query = searchWithinPanel.getQuery();
			if (query.length() != 0) searchResultsLabel.setText(entryName()+" Search Results for '" + query +"'"); 
			else searchResultsLabel.setText("Viewing all " +entryName());
		}
	};
}
